package com.nan.conn;


import com.nan.util.HttpUtil;
import com.nan.util.Util;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;


public class Connection {

    public String sendMsg(String accessToken,String secret,String param)  {

        String result = null;
        try {
            Long timestamp = System.currentTimeMillis();
            String sign = getSign(timestamp,secret);

            result = HttpUtil.post("https://oapi.dingtalk.com/robot/send",accessToken
                    ,"application/json", timestamp.toString(), sign,param,"UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;

    }

    public String keyword(String accessToken,String secret,String param)  {

        String result = null;
        try {
            Long timestamp = System.currentTimeMillis();
            String sign = getSign(timestamp,secret);

            result = HttpUtil.post("https://oapi.dingtalk.com/robot/send",accessToken
                    ,"application/json", timestamp.toString(), sign,param,"UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;

    }


    public String code(String accessToken,String secret,String param) {

        String result = null;
        try {
            Long timestamp = System.currentTimeMillis();
            String sign = getSign(timestamp,secret);

             result = HttpUtil.post("https://oapi.dingtalk.com/robot/send", accessToken
                     ,"application/json", timestamp.toString(), sign, param, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;

    }

    public String IP(String accessToken,String secret,String param)  {

        String result = null;
        try {
            Long timestamp = System.currentTimeMillis();
            String sign = getSign(timestamp,secret);

            result = HttpUtil.post("https://oapi.dingtalk.com/robot/send",accessToken
                    ,"application/json", timestamp.toString(), sign,param,"UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    public String IPRange(String accessToken,String secret,String param)  {

        String result = null;
        try {
            Long timestamp = System.currentTimeMillis();
            String sign = getSign(timestamp,secret);

            result = HttpUtil.post("https://oapi.dingtalk.com/robot/send",accessToken
                    ,"application/json", timestamp.toString(), sign,param,"UTF-8");
            System.out.println(result);   } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    public String allWays(String accessToken,String secret,String param) {

        String result = null;
        try {
            Long timestamp = System.currentTimeMillis();
            String sign = getSign(timestamp,secret);

            result = HttpUtil.post("https://oapi.dingtalk.com/robot/send", accessToken
                    , "application/json", timestamp.toString(), sign, param, "UTF-8");
            System.out.println(result); } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 自定义机器人获取签名
     * 创建机器人时选择加签获取secret以SE开头
     *
     * @param secret
     * @return
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException
     * @throws InvalidKeyException
     */
    private String getSign(Long timestamp,String secret) throws NoSuchAlgorithmException
            , UnsupportedEncodingException, InvalidKeyException {

        return Util.getSign(timestamp,secret);
    }
}
